﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Qiang.Data;

namespace Qiang.Styles
{
    public class VectorLayer : Layer,IVectorLayer
    {

        #region 初始化

        /// <summary>
        /// 自定义矢量图层
        /// </summary>
        /// <param name="dataset"></param>
        public VectorLayer(IVectorSet dataset)
            :base(dataset)
        {
            this.Dataset = dataset;
            Configure();
        }



        void Configure()
        {
            Visible = true;
            Selectable = true;
            _isShowLabel = true;
            _symbol = new VectorSymbol();
            _selectionSymbol = new VectorSymbol();
            ShapeState state = new ShapeState(_symbol, _selectionSymbol, false);
            _filter = new ShapeFilter(Dataset.ShapeList, state);
            _selection = new VectorSelection(_filter);
        }


        #endregion


        #region 字段

        /// <summary>
        /// 是否显示标注
        /// </summary>
        bool _isShowLabel;
       
        /// <summary>
        /// 图层所选内容
        /// </summary>
        private IVectorSelection _selection;

       
        #endregion


        #region 属性
        

        /// <summary>
        /// 是否显示标注
        /// </summary>
        public bool IsShowLabel
        {
            get { return _isShowLabel; }
            set
            {
                _isShowLabel = value;
                OnLayerContentChanged(); 
            }
        }

        /// <summary>
        /// 数据集
        /// </summary>
        public new IVectorSet Dataset
        {
            get
            {
                return base.Dataset as IVectorSet;
            }
            set
            {
                if (Dataset != null)
                {
                    RemoveDataSetEvent(Dataset);
                }

                base.Dataset = value;
                HandleDataSetEvent(Dataset);
            }
        }

       
        /// <summary>
        /// 图层所选内容
        /// </summary>
        public new IVectorSelection Selection
        {
            get
            {
                return _selection;
            }
        }


        IVectorSymbol _symbol;

        /// <summary>
        /// 
        /// </summary>
        public IVectorSymbol Symbol 
        {
            get { return _symbol; }
            set 
            { 
                _symbol = value;
                foreach (KeyValuePair<IShape, ShapeState> item in _filter.VectorStates)
                {
                    item.Value.Symbol = value;
                }
                OnLayerContentChanged();
            }
        }

        IVectorSymbol _selectionSymbol;

        /// <summary>
        /// 
        /// </summary>
        public IVectorSymbol SelectionSymbol
        {
            get { return _selectionSymbol; }
            set 
            { 
                _selectionSymbol = value;
                foreach (KeyValuePair<IShape, ShapeState> item in _filter.VectorStates)
                {
                    item.Value.SelectionSymbol = value;
                }
                OnLayerContentChanged();
            }
        }


        ShapeFilter _filter;

        /// <summary>
        /// 
        /// </summary>
        public ShapeFilter Filter
        {
            get { return _filter; }
            set { _filter = value; }
        }

     
        #endregion


        #region 私有方法

        /// <summary>
        /// 订阅数据集的事件
        /// </summary>
        /// <param name="dataset"></param>
        void HandleDataSetEvent(IVectorSet dataset)
        {                        
            if(dataset ==null ) return ;
            dataset.ShapeAdded += DataSetShapeAdded;
            dataset.ShapeRemoved += DataSetShapeRemoved;
            dataset.ShapeChanged += DataSetShapeChanged;
        }

        /// <summary>
        /// 取消事件订阅
        /// </summary>
        /// <param name="dataset"></param>
        void RemoveDataSetEvent(IVectorSet dataset)
        {
            if (dataset == null) return;
            dataset.ShapeAdded -= DataSetShapeAdded;
        }


        void DataSetShapeAdded(object sender, ShapeEventArgs args)
        {
            _filter.VectorStates.Add(args.Shape, new ShapeState(Symbol, SelectionSymbol, false));
            OnDatasetContentChanged();
        }

        void DataSetShapeRemoved(object sender, ShapeEventArgs args)
        {
            _filter.VectorStates.Remove(args.Shape);
            OnDatasetContentChanged();
        }

        void DataSetShapeChanged(object sender, ShapeEventArgs args)
        {
            OnDatasetContentChanged();
        }


      

        #endregion


        #region 继承的方法

        /// <summary>
        /// 获取图层范围
        /// </summary>
        /// <returns></returns>
        protected override IExtent ComputeExtent()
        {
            return Dataset.Extent;
        }

        /// <summary>
        /// 触发数据集内容改变事件
        /// </summary>
        protected override void OnDatasetContentChanged()
        {
            base.OnDatasetContentChanged();
        }

        protected override void OnLayerContentChanged()
        {
            base.OnLayerContentChanged();
        }


        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        #endregion


        #region 选择

        /// <summary>
        /// 选择
        /// </summary>
        /// <param name="ext"></param>
        public override void Select(IExtent ext)
        {
            Selection.Select(ext);
        }

        public override void Select(IExtent ext, SelectionMode Mode)
        {
            Selection.Select(ext, Mode);
        }


        /// <summary>
        /// 
        /// </summary>
        public override void ClearSelected()
        {
            Selection.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        public override void RemoveSelected()
        {
            IList<IShape> shps = Selection.ToList();
            foreach (IShape shp in shps)
            {
                Dataset.RemoveShape(shp);
            }
        }


        #endregion

    }
}
